package main

import (
	"SocketTest/ui"
	"gioui.org/app"
	"gioui.org/op"
	"gioui.org/op/clip"
	"log"
	"os"
)

type Config struct {
}

type SocketTest struct {
	ui     *ui.SocketTestUI
	config *Config
}

func (rec *SocketTest) Run(w *app.Window) error {
	var ops op.Ops

	for {
		switch e := w.Event().(type) {
		case app.DestroyEvent:
			return e.Err
		case app.FrameEvent:
			gtx := app.NewContext(&ops, e)
			area := clip.Rect{Max: gtx.Constraints.Max}.Push(&ops)

			rec.ui.Layout(gtx)

			area.Pop()
			e.Frame(&ops)
		}
	}
}

func main() {
	w := new(app.Window)

	testUI := ui.NewSocketTestUI(w)
	testUI.InitEventHandlers()
	socketTest := &SocketTest{ui: testUI, config: &Config{}}

	w.Option(app.Title("SocketTest"), app.Size(800, 600))

	go func() {
		if err := socketTest.Run(w); err != nil {
			log.Println(err)
		}
		os.Exit(1)
	}()

	app.Main()
}
